package com.example.demojimmer.application.in.rest.controller;


import com.example.demojimmer.application.dto.Results;
import com.example.demojimmer.domain.userauth.model.entity.StPermission;
import com.example.demojimmer.domain.userauth.model.entity.StPermissionFetcher;
import com.example.demojimmer.domain.userauth.model.entity.dto.SimpStPermissionView;
import com.example.demojimmer.domain.userauth.services.PermissionService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.client.FetchBy;
import org.babyfish.jimmer.sql.fetcher.Fetcher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
public class PermissionController {
    private final PermissionService permissionService;

    //@Tag() 用于说明或定义的标签
    //@Tag(name = "刷新系统权限到权限表")
    //@Schema 用于描述实体
    @PreAuthorize("hasAuthority('/permission/refreshPreAuthorizeToPermission')")
    @PostMapping("/refreshPreAuthorizeToPermission")
    public Results<Object> refreshPreAuthorizeToPermission() {
        permissionService.refreshPreAuthorizeToPermission();
        return Results.ok(null);
    }


    //@Tag(name = "查看全部权限")
    //@Schema 用于描述实体
    @PreAuthorize("hasAuthority('/permission/queryall')")
    @PostMapping("/queryall")
    public Results<List<@FetchBy("queryallFetcher") StPermission>> queryall() {

        List<StPermission> queryall = permissionService.queryall(queryallFetcher);
        return Results.ok(queryall);
    }

    private static final Fetcher<StPermission> queryallFetcher = StPermissionFetcher.$
            .permissionName()
            .permissionVal()
            .permissionType();


    //@Tag(name = "查看全部权限")
    //@Schema 用于描述实体
    @PreAuthorize("hasAuthority('/permission/queryalldto')")
    @PostMapping("/queryalldto")
    public Results<List<SimpStPermissionView>> queryalldto() {

        List<SimpStPermissionView> queryalldto = permissionService.queryalldto(SimpStPermissionView.class);
        return Results.ok(queryalldto);
    }

}

